package com.hwl.demo3;

import com.hwl.demo3.component.CashNormal;
import com.hwl.demo3.component.CashRebate;
import com.hwl.demo3.component.CashReturn;
import com.hwl.demo3.service.ISale;

import java.util.Scanner;

/**
 * @author sentry
 * @since 2023-02-28
 */
public class CashContext {

    private final ISale cs;

    public CashContext(int cashType) {
        switch (cashType) {
            case 2:
                this.cs = new CashRebate(0.8d);
                break;
            case 3:
                this.cs = new CashRebate(0.5d);
                break;
            case 4:
                this.cs = new CashReturn(300d, 100d);
                break;
            case 5:
                // 先打8折，再满300减100
                CashNormal cn = new CashNormal();
                CashReturn cr1 = new CashReturn(300d, 100d);
                CashRebate cr2 = new CashRebate(0.8d);

                cr1.decorate(cn);
                cr2.decorate(cr1);
                this.cs = cr2;
                break;
            case 6:
                // 先满200减20，再打7折
                CashNormal cn2 = new CashNormal();
                CashRebate cr3 = new CashRebate(0.7d);
                CashReturn cr4 = new CashReturn(200d, 50d);

                cr3.decorate(cn2);
                cr4.decorate(cr3);
                this.cs = cr4;
                break;
            default:
                this.cs = new CashNormal();
        }
    }

    public double getResult(double price, int num) {
        return this.cs.acceptCash(price, num);
    }

    public static void main(String[] args) {
        int cashType;
        double price;
        int num;
        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入商品折扣方式：");
            cashType = Integer.parseInt(sc.nextLine());
            System.out.println("请输入商品单价：");
            price = Double.parseDouble(sc.nextLine());
            System.out.println("请输入商品数量：");
            num = Integer.parseInt(sc.nextLine());
        } catch (Exception e) {
            throw new RuntimeException("输入有误 " + e);
        }

        double result = new CashContext(cashType).getResult(price, num);
        System.out.println("需要支付：" + result + "元");
    }
}
